package com.zb.mq;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zb.config.MQConfig;
import com.zb.entity.TbLearningCourseModel;
import com.zb.entity.TbTaskHisModel;
import com.zb.service.TbLearningCourseService;
import com.zb.service.TbTaskHisService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * <p>添加描述信息</p>
 *
 * @author 李柯
 * @since 2022年11月25日 14:03
 */
@Component
public class TaskListener {

    @Autowired
    private TbLearningCourseService tbLearningCourseService;

    @Autowired
    private TbTaskHisService tbTaskHisService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = "addCourseQueue")
    public void reviceTask(String json){
        System.out.println("监听开启.....");
        System.out.println(json);
        TbTaskHisModel tbTaskHisModel = JSON.parseObject(json, TbTaskHisModel.class);
        this.addCourse(tbTaskHisModel);
        //回送的mq
        rabbitTemplate.convertAndSend(MQConfig.ADD_COURSE_SUCCESS_EXCHANGE, MQConfig.ADD_COURSE_SUCCESS_ROUTING, JSON.toJSONString(tbTaskHisModel));

    }


    @Transactional
    public void addCourse(TbTaskHisModel tbTaskHisModel) {
        String requestBody = tbTaskHisModel.getRequestBody();
        Map<String, Object> data = JSON.parseObject(requestBody, Map.class);

        QueryWrapper<TbLearningCourseModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbLearningCourseModel::getUserId, data.get("user_id"))
                .eq(TbLearningCourseModel::getCourseId, data.get("course_id"));
        //检查是否存在，幂等性验证
        TbLearningCourseModel one = tbLearningCourseService.getOne(queryWrapper);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (ObjectUtils.isEmpty(one)) {
                System.out.println("添加.....");
                one = new TbLearningCourseModel();
                one.setUserId(data.get("user_id").toString());
                one.setCourseId(data.get("course_id").toString());
                one.setEndTime(sdf.parse(data.get("endTime").toString()));
                one.setStartTime(sdf.parse(data.get("startTime").toString()));
                one.setPrice(new BigDecimal(data.get("price").toString()));
                one.setStatus("1");
                tbLearningCourseService.save(one);
            } else {
                System.out.println("修改......");
                one.setEndTime(sdf.parse(data.get("endTime").toString()));
                one.setStartTime(sdf.parse(data.get("startTime").toString()));
                one.setPrice(new BigDecimal(data.get("price").toString()));
                tbLearningCourseService.updateById(one);
            }
            //添加历史记录
            TbTaskHisModel byId = tbTaskHisService.getById(tbTaskHisModel.getId());
            if (ObjectUtils.isEmpty(byId)) {
                tbTaskHisService.save(tbTaskHisModel);
            } else {
                tbTaskHisService.updateById(tbTaskHisModel);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
